package com.pri.chat.base;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.netease.nim.uikit.net.SharedHelper;
import com.netease.nim.uikit.net.StatusBarUtil;
import com.netease.nim.uikit.net.entity.BannerBean;
import com.netease.nim.uikit.net.entity.BannerModel;
import com.netease.nim.uikit.net.entity.BaseBean;
import com.netease.nim.uikit.net.entity.NoticeUnreadNumber;
import com.netease.nim.uikit.net.rxjava.HttpMethods;
import com.netease.nim.uikit.net.subscribers.ProgressSubscriber;
import com.netease.nim.uikit.net.subscribers.SubscriberOnNextListener;
import com.pri.chat.MainActivity;
import com.pri.chat.MainDaoHangActivity;
import com.pri.chat.R;
import com.pri.chat.activity.DaoHangActivity;
import com.pri.chat.activity.LoginActivity;
import com.pri.chat.application.APP;
import com.pri.chat.bean.KeyAndValueEntity;
import com.pri.chat.database.greendao.KeyAndValueEntityDao;
import com.pri.chat.utils.Des3Util;
import com.pri.chat.utils.StringUtils;
import com.vondear.rxtool.RxActivityTool;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import androidx.appcompat.app.AppCompatActivity;
import okhttp3.MediaType;
import okhttp3.RequestBody;


public class SplashActivity extends AppCompatActivity {
    private SharedHelper sharedHelper;
    private Context mContext;

    private ImageView img;
    private TextView dumiao;
    private boolean isFinish = false;
    private  boolean isFirst;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setDarkMode(this);
        Log.i("time:Wel", System.currentTimeMillis() + "");
        //禁止锁屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //透明导航栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        setContentView(R.layout.activity_splash);
        mContext = this;
        img = findViewById(R.id.img);
        dumiao = findViewById(R.id.dumiao);

        SharedPreferences sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        isFirst = sp.getBoolean("isFirst", true);

        time = new TimeCount(4000, 1000);
        httpGetBanner();

        dumiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
//                RxActivityTool.skipActivity(SplashActivity.this, MainActivity.class);

                if (isFirst) {
                    Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    startActivity(intent);
                } else {
                    RxActivityTool.skipActivity(SplashActivity.this, MainActivity.class);
//                    if("1".equals(fbyw) && !"1".equals(showWish)){
//                        RxActivityTool.skipActivity(SplashActivity.this, DaoHangActivity.class);
//                    }else{
//                        RxActivityTool.skipActivity(SplashActivity.this, MainActivity.class);
//                    }
//                    List<KeyAndValueEntity> list = APP.getKeyAndValueEntityDao().queryBuilder().where(KeyAndValueEntityDao.Properties.TypeKey.eq("daohang")).build().list();
//                    if (list == null || list.size() == 0) {
//                        RxActivityTool.skipActivity(SplashActivity.this, MainDaoHangActivity.class);
//                    } else {
//                        if ("hasDone".equals(list.get(0).getValue())) {
//                            RxActivityTool.skipActivity(SplashActivity.this, MainActivity.class);
//                        } else {
//                            RxActivityTool.skipActivity(SplashActivity.this, MainDaoHangActivity.class);
//                        }
//                    }
                }
            }
        });

        List<KeyAndValueEntity> keyAndValueList = APP.getKeyAndValueEntityDao().queryBuilder().where(KeyAndValueEntityDao.Properties.TypeKey.eq("unreadnum")).build().list();
        if(keyAndValueList == null || keyAndValueList.size() == 0){
            NoticeUnreadNumber unreadNumber = new NoticeUnreadNumber();
            unreadNumber.setGzNum(0);
            unreadNumber.setPlNum(0);
            unreadNumber.setWishNum(0);
            unreadNumber.setXttzNum(0);
            unreadNumber.setZanNum(0);
            KeyAndValueEntity keyAndValueEntity = new KeyAndValueEntity();
            keyAndValueEntity.setTypeKey("unreadnum");
            keyAndValueEntity.setValue(new Gson().toJson(unreadNumber));
            APP.getKeyAndValueEntityDao().save(keyAndValueEntity);
        }

        fbyw = SharedHelper.readValueByKeyForString(this,"fbyw");
        showWish = SharedHelper.readValueByKeyForString(this,"showWish");
    }

    private String fbyw;
    private String showWish;
    private TimeCount time;
    private String path;

    @Override
    protected void onResume() {
        super.onResume();
        time.start();
    }

    Handler handler = new Handler();

    private void httpGetBanner() {
        SharedPreferences sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        boolean isFirst = sp.getBoolean("isFirst", true);
        SubscriberOnNextListener<BaseBean<List<BannerBean>>> onNextListener = baseBean -> {
            if (baseBean.getData() != null && baseBean.getData().size() > 0) {
                String path = baseBean.getData().get(0).getBannerPath();
                Glide.with(mContext)
                        .asDrawable()
                        .load(path)
                        .into(img);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        img.setVisibility(View.VISIBLE);
                        dumiao.setVisibility(View.VISIBLE);
                        time.start();
                    }
                },2000);
            } else {
                initTimer();
            }
        };
        BannerModel evaListModel = new BannerModel("welcomeBanner");
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(evaListModel));
        HttpMethods.getInstance().getBannerList(new ProgressSubscriber<>(onNextListener, mContext, false), requestBody);
    }

    private void initTimer() {
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            private Intent intent;
            @Override
            public void run() {
                if (isFirst) {
                    intent = new Intent(SplashActivity.this, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    startActivity(intent);
                } else {
                    RxActivityTool.skipActivity(SplashActivity.this, MainActivity.class);
//                    if("1".equals(fbyw) && !"1".equals(showWish)){
//                        RxActivityTool.skipActivity(SplashActivity.this, DaoHangActivity.class);
//                    }else{
//                        RxActivityTool.skipActivity(SplashActivity.this, MainActivity.class);
//                    }
//                    List<KeyAndValueEntity> list = APP.getKeyAndValueEntityDao().queryBuilder().where(KeyAndValueEntityDao.Properties.TypeKey.eq("daohang")).build().list();
//                    if (list == null || list.size() == 0) {
//                        RxActivityTool.skipActivity(SplashActivity.this, MainDaoHangActivity.class);
//                    } else {
//                        if ("hasDone".equals(list.get(0).getValue())) {
//                            RxActivityTool.skipActivity(SplashActivity.this, MainActivity.class);
//                        } else {
//                            RxActivityTool.skipActivity(SplashActivity.this, MainDaoHangActivity.class);
//                        }
//                    }
                }
                finish();
            }
        };
        timer.schedule(task, 0);
    }

    class TimeCount extends CountDownTimer {

        TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            dumiao.setText("跳过 " + (millisUntilFinished / 1000) + "S");
        }

        @Override
        public void onFinish() {
            if (!isFinish) {
                SharedPreferences sp = getSharedPreferences("login", Context.MODE_PRIVATE);
                boolean isFirst = sp.getBoolean("isFirst", true);
                if (isFirst) {
                    Intent intent = new Intent(mContext, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    startActivity(intent);
                    finish();
                } else {
                    RxActivityTool.skipActivity(SplashActivity.this, MainActivity.class);
//                    if("1".equals(fbyw) && !"1".equals(showWish)){
//                        RxActivityTool.skipActivity(SplashActivity.this, DaoHangActivity.class);
//                    }else{
//                        RxActivityTool.skipActivity(SplashActivity.this, MainActivity.class);
//                    }
//                    List<KeyAndValueEntity> list = APP.getKeyAndValueEntityDao().queryBuilder().where(KeyAndValueEntityDao.Properties.TypeKey.eq("daohang")).build().list();
//                    if (list == null || list.size() == 0) {
//                        RxActivityTool.skipActivity(mContext, MainDaoHangActivity.class);
//                    } else {
//                        if ("hasDone".equals(list.get(0).getValue())) {
//                            RxActivityTool.skipActivity(mContext, MainActivity.class);
//                        } else {
//                            RxActivityTool.skipActivity(mContext, MainDaoHangActivity.class);
//                        }
//                    }
                    finish();
                }
                dumiao.setText("跳过 0S");
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isFinish = true;
        time.cancel();
    }
}
